package com.example.forumsystem.services.impl;

import com.example.forumsystem.common.AppResult;
import com.example.forumsystem.common.ResultCode;
import com.example.forumsystem.dao.ArticleReplyMapper;
import com.example.forumsystem.exception.ApplicationException;
import com.example.forumsystem.model.Article;
import com.example.forumsystem.model.ArticleReply;
import com.example.forumsystem.services.IArticleReplyService;
import com.example.forumsystem.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleReplyServiceImpi implements IArticleReplyService {


    @Resource
    private ArticleReplyMapper articleReplyMapper;

    @Resource
    private ArticleServiceImpi articleServiceImpi;


    @Override
    public void create(ArticleReply articleReply) {
        //参数校验
        if (articleReply == null || articleReply.getArticleId() == null
                || articleReply.getPostUserId() == null || StringUtil.isEmpty(articleReply.getContent())) {
            // 打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }

        // 获取帖子
        Article article = articleServiceImpi.selectById(articleReply.getArticleId());
        // 校验帖子状态
        if(article == null || article.getDeleteState() == 1) {
            // 打印日志
            log.info(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }


        // 校验是否被封禁
        if(article.getState() == 1) {
            // 打印日志
            log.info(ResultCode.FAILED_ARTICLE_BANNED.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }

        // 对象初始化
        articleReply.setLikeCount(0);
        articleReply.setPostUserId(articleReply.getPostUserId());
        articleReply.setState((byte) 0);
        articleReply.setDeleteState((byte) 0);
        Date date = new Date();
        articleReply.setCreateTime(date);
        articleReply.setUpdateTime(date);

        // 调用DAO 添加回复帖子
        articleReplyMapper.insertSelective(articleReply);

        // 帖子回复数量+1
        articleServiceImpi.addOneReplyCountById(article.getId());
    }

    @Override
    public List<ArticleReply> selectByArticleId(Long articleId) {
        // 参数校验
        if(articleId == null || articleId <= 0) {
            // 打印日志
            log.info(ResultCode.FAILED_PARAMS_VALIDATE_FAILED_UPDATE.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE_FAILED_UPDATE));
        }

        // 获取文章对象
        Article article = articleServiceImpi.selectById(articleId);
        if(article == null || article.getState() == 1 || article.getDeleteState() == 1) {
            // 打印日志
            log.info(ResultCode.FAILED_ARTICLE_BANNED.toString());
            // 抛异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }

        // 调用DAO获取回复列表
        List<ArticleReply> result = articleReplyMapper.selectByArticleId(articleId);
        if(result == null) {
            result = new ArrayList<>();
        }

        return result;
    }
}
